//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.PageFlow;
using WCSFContrib.PageFlow.Xml.Tests.Mocks;
using System.Globalization;

namespace WCSFContrib.PageFlow.Xml.Tests
{
   [TestClass]
    public class XmlPageFlowDefinitionFixture
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PageFlowTypeCannotBeNull()
        {
            new XmlPageFlowDefinition(null);
        }

        [TestMethod]
        public void CanCreateInstance()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();

            Assert.IsNotNull(definition);
            Assert.IsTrue(definition is IPageFlowDefinition);
        }

        [TestMethod]
        public void ConstrainedShouldBeEqualsXmlTypeConstrained()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);
            Assert.AreEqual(definition.Constrained, !pageFlowConfiguration.Constrained);
        }

        [TestMethod]
        public void NameShouldBeEqualsXmlTypeName()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);
            Assert.AreEqual("Unconstrained", definition.Name);
        }

        [TestMethod]
        public void ContainsPage()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();
            bool containsPage = definition.ContainsPage(new Page("pageA", "~/pageA.aspx"));

            Assert.IsTrue(containsPage);
        }

        [TestMethod]
        public void ContainsUrl()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();
            bool containsUrl = definition.ContainsUrl("~/pageA.aspx");

            Assert.IsTrue(containsUrl);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void PageArgumentContainsPageCannotBeNull()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();
            definition.ContainsPage(null);
        }

        [TestMethod]
        public void StartPageShouldBeXmlTypeInitialStateName()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.StartView.Name, definition.StartPage.Name);
        }

        [TestMethod]
        public void AbandonableShouldEqualsXmlTypeAbandonable()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.Abandonable, definition.Abandonable);
        }

        [TestMethod]
        public void AbortPageUrlShouldBeEqualsWorkflowFoundationTypeAbortUrl()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);
            Assert.AreEqual(pageFlowConfiguration.AbortUrl, definition.AbortPage.Url);
        }

        [TestMethod]
        public void NotRunningUrlShouldBeEqualsWorkflowFoundationTypeNotRunningUrl()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);
            Assert.AreEqual(pageFlowConfiguration.NotRunningUrl, definition.NotRunningRedirect);
        }

        [TestMethod]
        public void NumberOfPagesShouldBeEqualsXmlTypePageActivityCount()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.Views.Count, definition.Pages.Count);
            Assert.AreEqual(pageFlowConfiguration.Views[0].Name, definition.Pages[0].Name);
            Assert.IsNotNull(pageFlowConfiguration.TryGetViewFromName(definition.Pages[1].Name));
        }


        [TestMethod]
        public void PageFlowTypeShouldBeTypeOfWorkflowFoundationDefinition()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(typeof(MockUnconstrainedPageFlow), definition.PageFlowType);
        }

        [TestMethod]
        public void ShouldKnowIfContainsAnSpecificUrl()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();

            Assert.IsFalse(definition.ContainsUrl("~/notContained.aspx"));
        }

        [TestMethod]
        public void UrlContainsUrlCouldBeNull()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();

            Assert.IsFalse(definition.ContainsUrl(null));
        }

        [TestMethod]
        public void ShouldReturnAPageFromItsName()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.StartView.Name, definition.GetPage(pageFlowConfiguration.StartView.Name).Name);
        }

        [TestMethod]
        public void ShouldReturnAPageFromItsNameEvenIfItsInDifferentCase()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.StartView.Name, definition.GetPage(pageFlowConfiguration.StartView.Name.ToUpper(CultureInfo.CurrentCulture)).Name);
        }

        [TestMethod]
        public void ShouldGetAPageWithAGivenUrl()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.StartView.Name, definition.GetPageFromUrl("~/pageA.aspx").Name);
        }


        [TestMethod]
        public void ShouldGetAPageWithAGivenUrlEvenIfItsOnDifferentCase()
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration;
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition(out pageFlowConfiguration);

            Assert.AreEqual(pageFlowConfiguration.StartView.Name, definition.GetPageFromUrl("~/PAGEA.ASPX").Name);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPageShouldThrowWhenPassedNullString()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();
            String s = null;
            Page p = definition.GetPage(s);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPageFromUrlShouldThrowWhenPassedNull()
        {
            IPageFlowDefinition definition = CreateUnconstrainedPageFlowDefinition();

            Page p = definition.GetPageFromUrl(null);
        }


        private static IPageFlowDefinition CreateUnconstrainedPageFlowDefinition()
        {
            return new XmlPageFlowDefinition(new MockUnconstrainedXmlPageFlowNavigationGraph());
        }

        private static IPageFlowDefinition CreateUnconstrainedPageFlowDefinition(out MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration)
        {
            pageFlowConfiguration = new MockUnconstrainedXmlPageFlowNavigationGraph();
            return new XmlPageFlowDefinition(pageFlowConfiguration);
        }

        private static IPageFlowDefinition CreatePageFlowDefinition(Type pageFlowType)
        {
            MockUnconstrainedXmlPageFlowNavigationGraph pageFlowConfiguration = new MockUnconstrainedXmlPageFlowNavigationGraph();
            pageFlowConfiguration.PageFlowType = pageFlowType;
            return new XmlPageFlowDefinition(pageFlowConfiguration);
        }

    }
}
